package com.zsk.gen;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.velocity.VelocityContext;

/**
 * 生成entity
 * 
 * @author keke
 * @date 2020/07/30
 */
public class EntityGen {

    private List<EntityProperty> colList;
    GenConfig config;
    String tableName;
    String comment;

    public EntityGen(String tableName, String comment, List<EntityProperty> colList, GenConfig config) {
        super();
        this.tableName = tableName;
        this.colList = colList;
        this.config = config;
        this.comment = comment;
    }

    public void gen() {
        if (colList == null) {
            return;
        }

        String entityName = GenUtil.transUnderlineToHump(tableName, true);
        String packageName = config.getBasePackage();
        if (GenUtil.isNotEmpty(config.getModuleName())) {
            packageName += "." + config.getModuleName();
        }
        packageName += "." + config.getEntityPackage();

        Set<String> setLib = new HashSet<>();
        StringBuilder sbLib = new StringBuilder();
        StringBuilder sbProperty = new StringBuilder();

        for (EntityProperty entity : colList) {

            String className = entity.getClassName();
            if ("java.sql.Timestamp".equals(className)) {
                entity.setClassName("java.time.LocalDateTime");
                entity.setType("LocalDateTime");
            } else if ("java.sql.Date".equals(className)) {
                entity.setClassName("java.time.LocalDate");
                entity.setType("LocalDate");
            }

            if (!setLib.contains(entity.getClassName())) {

                sbLib.append("import " + entity.getClassName() + ";\r\n");
                setLib.add(entity.getClassName());

            }

            // jpa 13种id策略见org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory
            if (entity.getName().equals("id")) {
                sbProperty.append("@Id\r\n" + "    @GeneratedValue(generator = \"uuid\")\r\n"
                    + "    @GenericGenerator(name = \"uuid\", strategy = \"uuid2\")\r\n"
                    + "    @ApiModelProperty(hidden = true)\r\n");
            }

            if (!entity.getName().equals("id")) {
                sbProperty.append("    @ApiModelProperty(value = \"" + entity.getComment() + "\")\r\n");
            }

            sbProperty.append("    private " + entity.getType() + " "
                + GenUtil.transUnderlineToHump(entity.getName(), false) + ";\r\n");
        }

        VelocityContext context = VelocityUtil.createContext();
        context.put("author", config.getAuthor());
        context.put("time", GenUtil.getYmd());
        context.put("package", packageName);
        context.put("tableName", tableName);
        context.put("tableComment", comment);
        context.put("entityName", entityName);
        context.put("importLib", sbLib.toString());
        context.put("importProperty", sbProperty.toString());

        String vmPath = "com/zsk/template/Entity.vm";
        String targetPath = "src/main/java/" + packageName.replaceAll("\\.", "/") + "/" + entityName + ".java";

        if (config.isCoverCode() || !GenUtil.fileExist(targetPath)) {
            VelocityUtil.build(vmPath, targetPath, context);
        }

    }

}
